<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    .box {
        background-color: red;
        width: 100px;
        height: 100px;
        position: absolute;
        /* left: 0;
        top: 0; */
    }
    .box1 {
        background-color: rgb(81, 255, 0);
        width: 100px;
        height: 100px;
        position: absolute;
        left: 100px;
        top: 100px;
    }
</style>

<body>
    <div>
        <div class="box"></div>
        <div class="box1"></div>
    </div>

    <script>
        var Box = document.getElementsByClassName('box')[0];
        var Box1 = document.getElementsByClassName('box1')[0];

        function moveFn(obj) {
            obj.onmousedown = function (ev) { //选择那个盒子
                var e = ev || window.event; //兼容IE
                //鼠标落点的位置
                var disX = e.clientX - obj.offsetLeft;
                var disY = e.clientY - obj.offsetTop;
                //在文档上添加鼠标移动事件
                document.onmousemove = function (ev) {
                    var e = ev || window.event; //兼容IE
                    obj.style.left = e.clientX - disX + "px";
                    obj.style.top = e.clientY - disY + "px";
                }
                obj.onmouseup = function () {
                    document.onmousemove = null
                }
            }
        }

        moveFn(Box);
        moveFn(Box1);
    </script>
</body>

</html>